﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Truextend.CoffeShop.Service;

namespace Truextend.CoffeShop.Shedulers
{
    class StartOrderReceptionTimerJob : SPJobDefinition
    {

        public StartOrderReceptionTimerJob():base()
        {
        }

        public StartOrderReceptionTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType):
            base(jobName, service, server, targetType)
        {
            
        }

        public StartOrderReceptionTimerJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Start Order Reception Timer Job";
        }

        public override void Execute(Guid targetInstanceId)
        {
            //base.Execute(targetInstanceId);                                                                       

            SPWebApplication webApplication = this.Parent as SPWebApplication;

            SPContentDatabase contentDatabase = webApplication.ContentDatabases[targetInstanceId];

            SPUserCollection userCollection = contentDatabase.Sites[0].AllWebs["CoffeeShop"].AllUsers;            

            List<string> allEmails = (from SPUser user in userCollection select user.Email).ToList();            

            var orderService = new OrderService();
            orderService.StartOrderReception(allEmails);            

        }

    }
}
